package com.taotao.service.impl;

import com.taotao.common.pojo.PictureResult;
import com.taotao.common.utils.FastDFSClient;
import com.taotao.service.PictureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class PictureServiceImpl implements PictureService {
	@Value("${IMAGE_SERVER_BASE_URL}")
	private String IMAGE_SERVER_BASE_URL;

	public PictureResult uploadPic(MultipartFile picFile) {
		PictureResult result = new PictureResult();
		if (picFile.isEmpty()) {
			result.setError(1);
			result.setMessage("图片为空");
			return result;
		}
		try {
			FastDFSClient fastDFSClient = new FastDFSClient(
					"classpath:properties/client.conf");

			String fileString = picFile.getOriginalFilename();
			String extName = fileString
					.substring(fileString.lastIndexOf(".") + 1);

			String url = fastDFSClient.uploadFile(picFile.getBytes(), extName);
			url = this.IMAGE_SERVER_BASE_URL + url;
			result.setError(0);
			result.setUrl(url);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(1);
			result.setMessage("图片上传失败");
		}
		return result;
	}
}
